package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.Result;
import com.cssl.pojo.TbItemCat;
import com.cssl.service.TbItemCatService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
//@CrossOrigin
public class TbItemCatController {
    @Resource
    private TbItemCatService service;

    /**
     * 商品分类添加
     * @param tbItemCat
     * @return
     */
    @PostMapping("/catInsert")
    public TbItemCat insert(@RequestBody TbItemCat tbItemCat){
        if(service.saveOrUpdate(tbItemCat))
            return tbItemCat;
        return null;
    }

    /**
     * 商品分类修改
     * @param tbItemCat
     * @return
     */
    @RequestMapping("/catUpdate")
    public Result<TbItemCat> updateMember(TbItemCat tbItemCat) {
        System.out.println("修改会员:" + tbItemCat);
        //根据id改
        service.updateById(tbItemCat);
        return Result.ok(service.getById(tbItemCat.getCat_id()));
    }

    /**
     * 商品分类删除
     * @param cat_id
     * @return
     */
    @DeleteMapping("/catDelete/api/{cat_id}")
    public Result delete(@PathVariable("cat_id") Integer cat_id){
        boolean success = service.removeById(cat_id);
        if (success) {
            return Result.ok();
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 商品分类分页
     * @param tbItemCat
     * @param index
     * @param size
     * @return
     */
    @GetMapping("/catPage")
    public Result page(TbItemCat tbItemCat,
                                 @RequestParam(name = "index") int index,
                                 @RequestParam(name = "size") int size){
        return Result.ok(service.page(new Page<TbItemCat>(index,size)));
    }
    /**
     * 根据商品id模糊查询
     * @param tbItemCat
     * @return
     */
//    @GetMapping("/catQuery")
//    public List<TbItemCat> query(TbItemCat tbItemCat){
//
//
//        return service.list(
//                new QueryWrapper<TbItemCat>()
//                        .like("getCat_name",tbItemCat.getCat_name())
//                        .orderByDesc("cat_id")
//        );
//    }

    /**
     * 根据分类名称分页查询
     * @param tbItemCat
     * @param index
     * @param size
     * @return
     */
    @GetMapping("/catQuery")
    public Result query(TbItemCat tbItemCat,
                        @RequestParam(name="index" ) int index,
                        @RequestParam(name="size") int size){
        System.out.println("dasdasdsa------------------");
        LambdaQueryWrapper<TbItemCat> lqw = Wrappers.lambdaQuery();
        lqw.like(tbItemCat.getCat_name()!=null,
                TbItemCat::getCat_name,tbItemCat.getCat_name());
        lqw.orderByDesc(TbItemCat::getCat_id);
        return Result.ok(service.page(
                new Page<TbItemCat>(index,size),lqw));
    }

    @GetMapping("/query")
    public Result queryApi(){
        return Result.ok(service.list());
    }

    @GetMapping("/catUpdateDelete")
    public Result catUpdateDelete(TbItemCat tbItemCat){
        return Result.ok(service.update(
                new UpdateWrapper<TbItemCat>()
                        .eq("cat_id",tbItemCat.getCat_id())//根据这个去修改
                        .set("cat_deleted",tbItemCat.getCat_deleted())));//只修改这个列
    }
}
